#ifndef     __MESSAGE_H
#define     __MESSAGE_H

#include "./proto.h"
#include "../utils/decode.h"

namespace conn {

class Message {

public:
    Message() {}
    Message(const uint8_t *b, uint8_t l) : buf(b), data_len(l) {}

    inline uint8_t type() const {
        return buf[0];
    }

    inline uint8_t seq() const {
        return buf[1];
    }

    inline uint8_t req_cmd() const {
        return buf[2];
    }
    
    inline uint8_t query_cmd() const {
        return buf[1];
    }

    template <typename... Ts>
    utils::Error parse_req(Ts&&... ts) const {
        using namespace utils;
        return (decode_all(buf + 3, data_len - 5, std::forward<Ts>(ts)...) ? ec::OK : ec::PARSE_FAIL);
    }

private:
    const uint8_t *buf;
    uint8_t data_len;
};

}


#endif
